package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.UserManagerDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组服务
 */
@Service(interfaceClass = UserManagerService.class)
@Transactional
public class UserManagerServiceImpl implements UserManagerService {
    @Autowired
    private UserManagerDao userManagerDao;
    //新增检查组，同时需要关联检查项（设置多对多关系）
    public void add(User user, Integer[] checkitemIds) {
        //新增检查组
        userManagerDao.add(user);
        Integer userId = user.getId();//检查组id

        //设置检查组和检查项多对多关系
        this.setUserAndRole(userId,checkitemIds);
    }

    private void setUserAndRole(Integer userId, Integer[] checkitemIds) {

    }

    //分页查询
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
       /* if (queryString.equals("男")){
            queryString = "1";
        }else{
            queryString = "2";
        }*/
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page = userManagerDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    public List<Integer> findCheckItemIdsByCheckGroupId(Integer userId) {
        return userManagerDao.findCheckItemIdsByCheckGroupId(userId);
    }

    //根据id删除检查项
    public void delete(Integer id) {
        long count = userManagerDao.selectCountByCheckItemId(id);
        if(count > 0){
            //已经被关联，不能删除
            throw new RuntimeException(MessageConstant.DELETE_CHECKITEM_FAIL);
        }else{
            //可以删除
            userManagerDao.deleteById(id);
        }
    }

    @Override
    public User findById(Integer id) {
        return userManagerDao.findById(id);
    }

     //编辑检查项信息，同时需要设置关联关系
    public void edit(User user, Integer[] checkitemIds) {
        //基本信息修改
        userManagerDao.edit(user);
        //清理检查组和检查项的关联关系（操作中间关系表）
        userManagerDao.deleteAssociation(user.getId());
        //重新建立检查组和检查项的关联关系
        this.setCheckGroupAndCheckItem(user.getId(),checkitemIds);
    }

    public List<User> findAll() {
        return userManagerDao.findAll();
    }

    //设置检查组和检查项多对多关系
    public void setCheckGroupAndCheckItem(Integer userId,Integer[] checkitemIds){
        //设置多对多关系
        if(checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {//检查项id
                Map<String,Integer> map = new HashMap<>();
                map.put("userId",userId);
                map.put("checkitemId",checkitemId);
                userManagerDao.setUserAndRole(map);
            }
        }
    }
}
